#!/bin/sh
# Start/stop/restart Rabbit MQ daemon

USER=rabbitmq
DAEMON_START_OPTS="-detached"

rabbitmq_start(){
	sudo -u ${USER} sh -c "/usr/bin/rabbitmq-server ${DAEMON_START_OPTS}"
}

rabbitmq_stop() {
	sudo -u ${USER} sh -c "/usr/bin/rabbitmqctl stop"
}

rabbitmq_restart() {
	rabbitmq_stop
	sleep 1
	rabbitmq_start
}

rabbitmq_status() {
	sudo -u ${USER} sh -c "/usr/bin/rabbitmqctl status"
}

print_usage() {
	echo "usage $0 start|stop|restart|status"
}

case "$1" in
	'start')
		rabbitmq_start
	      	;;
	'stop')
		rabbitmq_stop
	      	;;
	'restart')
		rabbitmq_restart
	      	;;
	'status')
		rabbitmq_status
		;;
	*)
		print_usage
		;;
esac
